package com.kasax.hilog;

import android.util.Log;

import com.kasax.hilog.printer.ILogPrinter;

public class Logger {


    public static void log(Object... args) {
        dlog(null, args);
    }

    public static void log(Throwable throwable) {
        dlog(throwable, null);
    }

    public static void log(Throwable throwable, Object... args) {
        dlog(throwable, args);
    }

    public static void dlog(Throwable throwable, Object... args) {
        LogConfig logConfig = LogManager.getLogConfig();
        if (!logConfig.enable()) {
            // 日志未启用，直接忽略
            return;
        }

        StringBuffer logMsgBuffer = new StringBuffer();
        logMsgBuffer.append("------------ start -----------").append("\r\n");

        // 线程信息格式化
        if (logConfig.includeThreadInfo()) {
            logMsgBuffer.append(logConfig.THREAD_LOG_FORMATTER.format(Thread.currentThread())).append("\r\n");
        }


        // 堆栈信息格式化
        if (logConfig.includeStackTrace()) {
            if (throwable != null && throwable.getStackTrace() != null && throwable.getStackTrace().length > 0) {
                logMsgBuffer.append(logConfig.STRACK_TRACE_FORMATTER.format(throwable.getStackTrace())).append("\r\n");
            }
        }

        logMsgBuffer.append(parseLogMessage(logConfig.injectJsonFrmatter(), args)).append("\r\n");

        logMsgBuffer.append("------------ end -----------").append("\r\n");

        ILogPrinter[] printers = LogManager.getPrinters();
        if (printers != null) {
            for (ILogPrinter printer : printers) {
                if (printer != null) {
                    printer.log(logConfig.getLogLevel(), logConfig, logMsgBuffer.toString());
                }
            }
        }
    }

    /**
     * 解析日志数据
     *
     * @param args
     * @return
     */
    private static String parseLogMessage(LogConfig.JsonFormatter jsonFormatter, Object[] args) {
        if (args == null || args.length == 0) {
            return "";
        }

        StringBuffer sb = new StringBuffer();

        // 优化使用json
        if (jsonFormatter != null) {
            for (Object arg : args) {
                if (arg == null) {
                    continue;
                }
                // json格式化出错，直接使用原始数据
                try {
                    sb.append(jsonFormatter.format(arg)).append("\r\n");
                } catch (Exception e) {
                    sb.append(arg).append("\r\n");
                }
            }
            sb.deleteCharAt(sb.length() - 1);
        } else {
            for (Object arg : args) {
                if (arg == null) {
                    continue;
                }
                sb.append(arg).append("\n");
            }
            sb.deleteCharAt(sb.length() - 1);
        }

        return sb.toString();
    }

    private static void defaultLogPrint(Throwable throwable, String msg) {
        if (throwable != null) {
            Log.d("DefaultLogger", msg, throwable);

        } else {
            Log.d("DefaultLogger", msg);
        }
    }
}
